<template>
  <BookSearch></BookSearch>
  <el-card class="booksearchcard">
    <el-table
      ref="multipleTableRef"
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      border="true"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="Date" width="120">
        <template #default="scope">{{ scope.row.date }}</template>
      </el-table-column>
      <el-table-column property="name" label="Name" width="120" />
      <el-table-column
        property="address"
        label="Address"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper, total, sizes"
      :total="total"
      class="paginationclass"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[2, 3, 5]"
      small
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: 'BookSarch',
}
</script>

<script lang="ts" setup>
import BookSearch from '@/components/BookSearch/index.vue'
import { ref } from 'vue'
const currentPage = ref(1)
const pageSize = ref(1)
// 总数
const total = ref(0)

const getBookList = () => {
  const list = tableData
  total.value = list.length
}
interface User {
  date: string
  name: string
  address: string
}
const handleSizeChange = (newPageSize) => {
  pageSize.value = newPageSize
  getBookList()
}
const handleCurrentChange = (newCurrentPage) => {
  currentPage.value = newCurrentPage
  getBookList()
}
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<User[]>([])
const toggleSelection = (rows?: User[]) => {
  if (rows) {
    rows.forEach((row) => {
      // TODO: improvement typing when refactor table
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-expect-error
      multipleTableRef.value!.toggleRowSelection(row, undefined)
    })
  } else {
    multipleTableRef.value!.clearSelection()
  }
}
const handleSelectionChange = (val: User[]) => {
  multipleSelection.value = val
}
const tableData: User[] = [
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-08',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-06',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-07',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
]
</script>

<style scoped>
.booksearchcard {
  margin-top: 15px;
}
.paginationclass {
  margin-top: 15px;
}
</style>
